<template>
	<view>
		<form @submit="formSubmit" @reset="formReset">
			<view class="from-block">
				<view class="from_title">
					基金名称
				</view>
				
				<input
				disabled
				v-model="jijin.name"
				style="border: none;background-color: #ebebeb;"
				class="from_input"/>
			</view>
			
			
			<view class="from-block">
				<view class="from_title">
					基金代码
				</view>
				
				<input
				v-model="jijin.code"
				:disabled = isOld
				@input="add"
				class="from_input"/>
			</view>
			
			<view class="from-block">
				<view class="from_title">
					购买金额
				</view>
				
				<input
				type="number"
				v-model="jijin.buyMoney"
				class="from_input"/>
			</view>
			
			
			<view class="from-block">
				<view class="from_title">
					购买日期
				</view>

				<picker mode="date" 
				:disabled = false
				:value="jijin.buyDay" 
				:start="startDate" 
				:end="endDate" @change="bindDateChange"
				class="picker_select">
					<view class="uni-input">{{jijin.buyDay}}</view>
				</picker>

			</view>
			
			
			<view class="from-block">
				<view class="from_title">
					可赎天数
				</view>
			
				<picker mode = "selector"
				:range="freeDayList" 
				range-key="value"
				:disabled = false
				:value="freeIndex" 
				@change="freeDayChange"
				class="picker_select">
					<view class="uni-input">{{freeDayList[freeIndex].value}}</view>
				</picker>
			
			</view>
			
			<view class="from-block">
				<view class="from_title">
					假期天数
				</view>
			
				<picker mode = "selector"
				:range="holidayList" 
				range-key="value"
				:disabled = false
				:value="holidayIndex" 
				@change="holidayChange"
				class="picker_select">
					<view class="uni-input">{{holidayList[holidayIndex].value}}</view>
				</picker>
			
			</view>
			
			<view class="form_botton">
				<button 
				size="mini"
				type="primary"
				form-type="submit"
				class="submit_botton">
				提交
				</button>
				<button
				size="mini"
				type="warn"
				class="reset_botton"
				form-type="reset">
				重置
				</button>
			</view>
		
		</form>
	</view>
</template>

<script>
	
	var _self
	
	export default {
		data() {
			return {
				isOld: false, // 添加现有的基金
				oldFreeDay: 7,
				oldFreeDayIndex: 0,
				freeIndex: 0,
				freeDayList: [
					{value:7},{value: 30}, 
					{value:60}, {value:120},
					{value:180},{value: 360}
				],
				oldHoliday: 0,
				oldHolidayIndex: 0,
				holidayIndex: 0,
				holidayList: [
					{value:0},{value: 1}, 
					{value:2},{value: 3},
					{value:4},{value: 5},
					{value:6},{value: 7},
					{value:8},{value: 9},
					{value:10}
				],
				holiday: 0,
				jijin: {
					code: '',
					name: '',
					buyDay: this.getDate({
						format: true
					}),
					buyMoney: '',
					freeDay: 7,
					status: 0	
				}
			}
		},
		computed: {
		    startDate() {
		        return this.getDate('start');
		    },
		    endDate() {
		        return this.getDate('end');
		    }
		},
		onLoad(data) {
			
			var name = data.name
			var code = data.code
			if (name != undefined && code != undefined &&
			name != '' && code != ''){
				this.jijin.name = name
				this.jijin.code = code
				this.isOld = true
			}
			
			_self = this;
			
			// 判断今天是星期几
			var week = new Date().getDay();
			
			if(week == 5){
				this.holiday = 2
				this.oldHolidayIndex =  2
				this.holidayIndex = 2
			}else if (week == 6){
				this.holiday = 1
				this.oldHolidayIndex = 1
				this.holidayIndex = 1
			}else{
				this.holiday = 0
				this.oldHolidayIndex =  0
				this.holidayIndex = 0
			}
			// console.log('今天是'+new Date());
			
			
		},
		methods: {
			disableInput() {
				uni.showModal({
					content: "无法修改",
					showCancel: false
				});
			},
			// 监听输入的基金代码
			async add(e) {
				// console.log(this.jijin.code)
				if(e.detail.value.length >= 6){
					console.log('e.detail.value:'+e.detail.value)
					uni.showLoading({
						title:'查询中...',
						mask:false
					})
					// 满6位后查询基金名称
					var name = await this.store.getNameByCode(e.detail.value)
					console.log(name)
					if(name === null
						|| name === undefined
						|| name.length === 0
					){
						uni.showModal({
							content: '请输入正确的基金代码！',
							showCancel: false,
							success:function (res) {
								 if (res.confirm) {
									 // console.log('用户点击确定');
									_self.jijin.name = ''
								} else if (res.cancel) {
									// console.log('用户点击取消');
								}
							}
						});
					}else {
						_self.jijin.name = name
					}
					uni.hideLoading()
				}
			},
			// 清空数据
			clean() {
				this.jijin.code = ''
				this.jijin.name = ''
				this.jijin.buyDay = this.getDate({
					format: true
				})
				this.jijin.buyMoney = ''
				this.jijin.isSell = 0
				this.jijin.isBuy = 1
				this.jijin.isOk = 0	
			},
			// 提交触发
			formSubmit: async function(e) {
				// console.log('form发生了submit事件，携带数据为：' + JSON.stringify(e.detail.value))
				// var formdata = e.detail.value
				
				if(
					this.jijin.code !== '' &&
					this.jijin.name !== '' &&
					this.jijin.buyDay !== '' &&
					this.jijin.buyMoney !== '' &&
					this.jijin.freeDay !== '' 
				){
					uni.showLoading({
						title:'购买中...',
						mask:false
					})
					//可赎天数加一
					this.jijin.freeDay += 1
					//可赎天数加假期天数
					this.jijin.freeDay += this.holiday
					var result = await this.store.buy(this.jijin)
					if(result === 'ok'){
						uni.showModal({
							content: '购买成功',
							showCancel: false,
							success: function (res) {
								if (res.confirm) {
									//清空数据
									_self.clean()
									uni.navigateBack({
										delta: 1
									})
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
					uni.hideLoading()
				}else {
					console.log(this.jijin)
					uni.showModal({
						content: '请填齐数据！',
						showCancel: false
					});
				}
				
				
			},
			formReset: function(e) {
				// console.log('清空数据')
			},
			
			
			bindDateChange: function(e) {
			    this.jijin.buyDay = e.target.value
			},
			
			freeDayChange(e) {
				this.freeIndex = e.detail.value
				this.jijin.freeDay = this.freeDayList[e.detail.value].value
				this.oldFreeDay = this.jijin.freeDay
				this.oldFreeDayIndex = e.detail.value
			},
			
			holidayChange(e) {
				this.holidayIndex = e.detail.value
				this.holiday = this.holidayList[e.detail.value].value
				this.oldHoliday = this.holiday
				this.oldHolidayIndex = e.detail.value
			},
			
			getDate(type) {
			    const date = new Date();
			    let year = date.getFullYear();
			    let month = date.getMonth() + 1;
			    let day = date.getDate();
				
			    if (type === 'start') {
			        year = year - 60;
			    } else if (type === 'end') {
			        year = year + 2;
			    }
			    month = month > 9 ? month : '0' + month;;
			    day = day > 9 ? day : '0' + day;
			    return `${year}-${month}-${day}`;
			}
			
			
			
		}
	}
</script>

<style>
	
	/* .uni-form-item .title {
		padding: 20rpx 0;
	} */
	
	@import url("form.css");
</style>
